package com.ruoyi.wms.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.wms.domain.bo.employee.AttendancePunchBo;
import com.ruoyi.wms.domain.entity.employee.AttendancePunch;
import com.ruoyi.wms.domain.vo.AttendancePunchVo;
import com.ruoyi.wms.mapper.AttendancePunchMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 考勤记录Service业务层处理
 *
 * @author 邓滔滔
 * @date 2024-12-01
 */
@RequiredArgsConstructor
@Service
public class AttendancePunchService {

    private final AttendancePunchMapper attendancePunchMapper;

    /**
     * 查询考勤记录
     */
    public AttendancePunchVo queryById(Long id){
        return attendancePunchMapper.selectVoById(id);
    }

    /**
     * 查询考勤记录列表
     */
    public TableDataInfo<AttendancePunchVo> queryPageList(AttendancePunchBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AttendancePunch> lqw = buildQueryWrapper(bo);
        Page<AttendancePunchVo> result = attendancePunchMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询考勤记录列表
     */
    public List<AttendancePunchVo> queryList(AttendancePunchBo bo) {
        LambdaQueryWrapper<AttendancePunch> lqw = buildQueryWrapper(bo);
        return attendancePunchMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AttendancePunch> buildQueryWrapper(AttendancePunchBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AttendancePunch> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getEmployeeId() != null, AttendancePunch::getEmployeeId, bo.getEmployeeId());
        lqw.eq(bo.getPunchType() != null, AttendancePunch::getPunchType, bo.getPunchType());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            AttendancePunch::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        return lqw;
    }

    /**
     * 新增考勤记录
     */
    public void insertByBo(AttendancePunchBo bo) {
        AttendancePunch add = MapstructUtils.convert(bo, AttendancePunch.class);
        attendancePunchMapper.insert(add);
    }

    /**
     * 修改考勤记录
     */
    public void updateByBo(AttendancePunchBo bo) {
        AttendancePunch update = MapstructUtils.convert(bo, AttendancePunch.class);
        attendancePunchMapper.updateById(update);
    }

    /**
     * 批量删除考勤记录
     */
    public void deleteByIds(Collection<Long> ids) {
        attendancePunchMapper.deleteBatchIds(ids);
    }
}
